﻿using MesErreurs;
using Metier;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Gestion_commerciale
{
    public partial class FArticle : Form
    {
        public FArticle()
        {
            InitializeComponent();
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
            AfficherListe();
        }

        private void AfficherListe()
        {
            List<Article> mesArticles;
            ListViewItem lvitem_cde;
            Article unArticle = new Article();
            lvcdes.Items.Clear();
            lvcdes.Columns.Clear();
            lvcdes.View = View.Details;
            lvcdes.Columns.Add("1", "Numéro Article");
            lvcdes.Columns.Add("2", "Nom Article");
            lvcdes.Columns.Add("5", "Prix Unitaire");
            lvcdes.Columns.Add("3", "Quantité Disponible");
            lvcdes.Columns.Add("4", "Ville Article");
            lvcdes.Columns.Add("6", "Interrompu");
            lvcdes.FullRowSelect = true;
            try
            {
                mesArticles=unArticle.getLesArticles();
                foreach (Article c in mesArticles)
                {
                    lvitem_cde = new ListViewItem(new string[] { c.NoArticle, c.LibArticle, c.PrixArticle.ToString(), c.QteDispo.ToString(), c.VilleArticle, c.Interrompu }, -1, Color.Black, Color.LightGreen, null);
                    lvcdes.Items.Add(lvitem_cde);
                }
                lvcdes.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            }
            catch (MonException erreur)
            {
                throw erreur;
            }
        }

        private void bt_Supprimer_Click(object sender, EventArgs e)
        {
            if (lvcdes.SelectedItems.Count > 0)
            {
                var confirmation = MessageBox.Show("Voulez vous vraiment supprimer l'article ?", "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (confirmation == DialogResult.Yes)
                {
                    Article article = new Article();
                    try
                    {
                        article.RechercheUnArticle(lvcdes.SelectedItems[0].SubItems[0].Text);
                        article.supprimerArticle();
                    }
                    catch (MonException erreur)
                    {
                        throw erreur;
                    }
                }
                AfficherListe();
            }
            else MessageBox.Show("Sélectionner un article");
        }

        private void bt_Ajouter_Click(object sender, EventArgs e)
        {
            FInser_Art finser = new FInser_Art();
            finser.ShowDialog();
            AfficherListe();
        }

        private void bt_Modifier_Click(object sender, EventArgs e)
        {
            if (lvcdes.SelectedItems.Count > 0)
            {
                FModif_Art fModif = new FModif_Art(lvcdes.SelectedItems[0].SubItems[0].Text);
                fModif.ShowDialog();
                AfficherListe();
            }
            else MessageBox.Show("Sélectionner un article");
        }
    }
}
